//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BlackVarianceCurve.h"
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVarianceTermStructure.h>
using namespace Cephei::QL::Times;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::CBlackVarianceCurve (boost::shared_ptr<QuantLib::BlackVarianceCurve>& childNative, Object^ owner) : CBlackVarianceTermStructure(CBlackVarianceCurve::typeid)
{
#ifdef HANDLE
	_phBlackVarianceCurve = NULL;
#endif
	_ppBlackVarianceCurve = &childNative;
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceCurve));
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::CBlackVarianceCurve (QuantLib::BlackVarianceCurve& childNative, Object^ owner) : CBlackVarianceTermStructure(CBlackVarianceCurve::typeid)
{
#ifdef HANDLE
	_phBlackVarianceCurve = NULL;
#endif
	_ppBlackVarianceCurve = new boost::shared_ptr<QuantLib::BlackVarianceCurve> (&childNative);
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceCurve));
    _BlackVarianceCurveOwner = owner;
    _BlackVarianceTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::CBlackVarianceCurve (CBlackVarianceCurve^ copy) : CBlackVarianceTermStructure(CBlackVarianceCurve::typeid)
{
#ifdef HANDLE
	_phBlackVarianceCurve = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackVarianceCurve = new boost::shared_ptr<QuantLib::BlackVarianceCurve> (copy->GetShared());
        _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceCurve));
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::CBlackVarianceCurve (PLATFORM::Type^ t) : CBlackVarianceTermStructure(CBlackVarianceCurve::typeid)
{
#ifdef HANDLE
	_phBlackVarianceCurve = NULL;
#endif
	if (!t->IsSubclassOf(CBlackVarianceCurve::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::CBlackVarianceCurve (QuantLib::Handle<QuantLib::BlackVarianceCurve>& childNative, Object^ owner)  : CBlackVarianceTermStructure(CBlackVarianceCurve::typeid)
{
	_phBlackVarianceCurve = &childNative;
	_ppBlackVarianceCurve = &static_cast<boost::shared_ptr<QuantLib::BlackVarianceCurve>>(childNative.currentLink());
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceCurve));
    _BlackVarianceCurveOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::CBlackVarianceCurve (QuantLib::Handle<QuantLib::BlackVarianceCurve> childNative)  : CBlackVarianceTermStructure(CBlackVarianceCurve::typeid)
{
	_phBlackVarianceCurve = &childNative;
	_ppBlackVarianceCurve = &static_cast<boost::shared_ptr<QuantLib::BlackVarianceCurve>>(childNative.currentLink());
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceCurve));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::CBlackVarianceCurve (QuantLib::BlackVarianceCurve childNative)  : CBlackVarianceTermStructure(CBlackVarianceCurve::typeid)
{
#ifdef HANDLE
	_phBlackVarianceCurve = NULL;
#endif
	_ppBlackVarianceCurve = new boost::shared_ptr<QuantLib::BlackVarianceCurve> (new QuantLib::BlackVarianceCurve (childNative));
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceCurve));
}
#endif

Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::~CBlackVarianceCurve ()
{
    if (_ppBlackVarianceCurve != NULL)
    {
	    delete _ppBlackVarianceCurve;
        _ppBlackVarianceCurve = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::!CBlackVarianceCurve ()
{
    if (_ppBlackVarianceCurve != NULL)
    {
	    delete _ppBlackVarianceCurve;
    }
}
QuantLib::BlackVarianceCurve& Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::GetReference ()
{
    if (_ppBlackVarianceCurve == NULL) throw REFNEW NativeNullException ();
	return **_ppBlackVarianceCurve;
}
boost::shared_ptr<QuantLib::BlackVarianceCurve>& Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::GetShared ()
{
    if (_ppBlackVarianceCurve == NULL) throw REFNEW NativeNullException ();
	return *_ppBlackVarianceCurve;
}
QuantLib::BlackVarianceCurve* Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::GetPointer ()
{
    if (_ppBlackVarianceCurve == NULL) throw REFNEW NativeNullException ();
	return &**_ppBlackVarianceCurve;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackVarianceCurve>& Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::GetHandle ()
{
	if (_phBlackVarianceCurve == NULL)
	{
		_phBlackVarianceCurve = new Handle<QuantLib::BlackVarianceCurve> (*_ppBlackVarianceCurve);
	}
	return *_phBlackVarianceCurve;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::HasNative () 
{
	return (_ppBlackVarianceCurve != NULL);
}

Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppBlackVarianceCurve)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppBlackVarianceCurve)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackVarianceCurve)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceCurve::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackVarianceCurve)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

